package com.oms.module.admin.controller.systemsettings;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oms.common.Page;
import com.oms.common.PageableVO;
import com.oms.common.ResponseDTO;
import com.oms.module.admin.constant.AdminResponseCodeConstant;
import com.oms.module.admin.constant.AnnouncementOrderEnum;
import com.oms.module.admin.constant.BuyerAuditParameterEnum;
import com.oms.module.admin.constant.BuyersAuditParameterOrderEnum;
import com.oms.module.admin.domain.entity.BusinessAuditParameterEntity;
import com.oms.module.admin.domain.entity.BuyersAuditParameterEntity;
import com.oms.module.admin.domain.vo.BuyersAuditParameterVO;
import com.oms.module.admin.service.BusinessAuditParameterService;
import com.oms.module.admin.service.BuyerAuditParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zhanghailong QQ38592077
 * @Description TODO
 * @Date 2020/6/18 11:31
 */
@RestController
@Api(value = "管理端审核意见管理")
@RequestMapping("/system/auditParameter")
public class AuditParameterController {
    @Autowired
    BuyerAuditParameterService buyerAuditParameterService;
    @Autowired
    BusinessAuditParameterService businessAuditParameterService;

    @GetMapping("/buyers")
    @ApiOperation(value = "买手审核意见列表 @author Zhang Hailong")
    public ResponseDTO<IPage<BuyersAuditParameterEntity>> getBuyersAuditParameterList(Integer current, Integer size, BuyersAuditParameterOrderEnum buyersAuditParameterOrderEnum) {
        Page<BuyersAuditParameterEntity> page = new Page(current, size);
        page = buyerAuditParameterService.page(page, Wrappers.<BuyersAuditParameterEntity>query().orderByDesc(buyersAuditParameterOrderEnum.getValue()));
        if (page.getTotal() > 0) {
            return ResponseDTO.succData(page);
        } else {
            return ResponseDTO.succMsg("没有再多数据了");
        }
    }

    @DeleteMapping("/buyers/{id}")
    @ApiOperation(value = "根据id删除买手审核意见 @author Zhl")
    public ResponseDTO deleteBuyersAuditParameterById(@PathVariable Long id) {
        boolean remove = buyerAuditParameterService.removeById(id);
        if (remove) {
            return ResponseDTO.succMsg("删除成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.DELETE_BUYER_AUDIT_ERROR);
    }

    @DeleteMapping("/buyers")
    @ApiOperation(value = "根据id批量删除买手审核意见 @author Zhl")
    public ResponseDTO deleteBuyersAuditParameterByIds(@RequestBody List<Long> id) {
        boolean remove = buyerAuditParameterService.removeByIds(id);
        if (remove) {
            return ResponseDTO.succMsg("删除成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.DELETE_BUYER_AUDIT_ERROR);
    }

    @PostMapping("/buyers")
    @ApiOperation(value = "添加买手审核意见 @author Zhl")
    public ResponseDTO addBuyersAuditParameter(@RequestBody @Validated BuyersAuditParameterVO buyersAuditParameterVO) {
        boolean insert = buyerAuditParameterService.save(new BuyersAuditParameterEntity(buyersAuditParameterVO.getContext(), buyersAuditParameterVO.getType()));
        if (insert) {
            return ResponseDTO.succMsg("添加成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.SAVE_BUYER_AUDIT_ERROR);
    }

    @PutMapping("/buyers")
    @ApiOperation(value = "修改买手审核意见 @author Zhl")
    public ResponseDTO putBuyersAuditParameterById(@RequestBody @Validated BuyersAuditParameterVO buyersAuditParameterVO) {
        boolean save = buyerAuditParameterService.update(
                Wrappers.<BuyersAuditParameterEntity>lambdaUpdate()
                        .set(BuyersAuditParameterEntity::getContext, buyersAuditParameterVO.getContext())
                        .set(BuyersAuditParameterEntity::getType, buyersAuditParameterVO.getType())
                        .eq(BuyersAuditParameterEntity::getId, buyersAuditParameterVO.getId())
        );
        if (save) {
            return ResponseDTO.succMsg("修改成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.MODIFY_BUYER_AUDIT_ERROR);
    }

    @GetMapping("/business")
    @ApiOperation(value = "商家审核意见列表 @author Zhl")
    public ResponseDTO<IPage<BusinessAuditParameterEntity>> getBusinessAuditParameterList(Integer current, Integer size, BuyersAuditParameterOrderEnum buyersAuditParameterOrderEnum) {
        Page<BusinessAuditParameterEntity> page = new Page(current, size);
        page = businessAuditParameterService.page(page, Wrappers.<BusinessAuditParameterEntity>query().orderByDesc(buyersAuditParameterOrderEnum.getValue()));
        if (page.getTotal() > 0) {
            return ResponseDTO.succData(page);
        } else {
            return ResponseDTO.succMsg("没有再多数据了");
        }
    }

    @DeleteMapping("/business")
    @ApiOperation(value = "根据id批量删除买手审核意见 @author Zhl")
    public ResponseDTO deleteBusinessAuditParameterByIds(@RequestBody List<Long> id) {
        boolean remove = businessAuditParameterService.removeByIds(id);
        if (remove) {
            return ResponseDTO.succMsg("删除成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.DELETE_BUSINESS_AUDIT_ERROR);
    }

    @PostMapping("/business")
    @ApiOperation(value = "添加买手审核意见 @author Zhl")
    public ResponseDTO addBusinessAuditParameter(@RequestBody @Validated BuyersAuditParameterVO buyersAuditParameterVO) {
        boolean insert = businessAuditParameterService.save(new BusinessAuditParameterEntity(buyersAuditParameterVO.getContext(), buyersAuditParameterVO.getType()));
        if (insert) {
            return ResponseDTO.succMsg("添加成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.SAVE_BUSINESS_AUDIT_ERROR);
    }

    @PutMapping("/business")
    @ApiOperation(value = "修改买手审核意见 @author Zhl")
    public ResponseDTO putBusinessAuditParameterById(@RequestBody @Validated BuyersAuditParameterVO buyersAuditParameterVO) {
        boolean save = businessAuditParameterService.update(
                Wrappers.<BusinessAuditParameterEntity>lambdaUpdate()
                        .set(BusinessAuditParameterEntity::getContext, buyersAuditParameterVO.getContext())
                        .set(BusinessAuditParameterEntity::getType, buyersAuditParameterVO.getType())
                        .eq(BusinessAuditParameterEntity::getId, buyersAuditParameterVO.getId())
        );
        if (save) {
            return ResponseDTO.succMsg("修改成功");
        }
        return ResponseDTO.wrap(AdminResponseCodeConstant.MODIFY_BUSINESS_AUDIT_ERROR);
    }

}
